package com.kimstore.dao.impl;

import com.kimstore.dao.UserDao;
import com.kimstore.bean.User;
import com.kimstore.utilities.Encryption;
import javax.persistence.Query;

public class UserDaoImpl extends GenericJpaDaoImpl<User, Integer> implements UserDao {

    @Override
    public User findByUsernamePassword(String username, String password) {
        Query query = getEntityManager().createQuery("from User user where user.username = :username and password = :password");
        query.setParameter("username", username);
        query.setParameter("password", Encryption.sha(password));
        
        User user = (User) getSingleResultOrNull(query);
        
        return user;
    }

    @Override
    public boolean existsUsername(String username) {
        Query query = getEntityManager().createQuery("select count(user) from User user where user.username = :username");
        query.setParameter("username", username);
        long count = (long) query.getSingleResult();
        
        return count > 0;
    }
    
        @Override
    public User findByUsername(String username) {
        Query query = getEntityManager().createQuery("from User user where user.username = :username");
        query.setParameter("username", username);
        User user = (User) query.getSingleResult();
        
        return user;
    }
}
